﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
namespace DALL.SysManage
{
    /// <summary>
    /// 数据访问类:T_PUB_PROJECTTYPE
    /// </summary>
    public partial class T_PUB_PROJECTTYPE
    {
        public T_PUB_PROJECTTYPE()
        { }

        /// <summary>
        /// 智能提示项目类型名称
        /// </summary>
        /// <returns></returns>
        public DataSet GetProjectTypeName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT PROJECTTYPE_NAME FROM T_PUB_PROJECTTYPE ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public List<Model.SysManage.T_PUB_PROJECTTYPE> GetAllListArry(string strPID)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("WITH lmenu(PROJECTTYPE_ID, PARENT_ID, IS_NULL, PROJECTTYPE_NAME,IDPATH, level) \n");
            varname1.Append("     AS (SELECT PROJECTTYPE_ID, \n");
            varname1.Append("                PARENT_ID, \n");
            varname1.Append("                IS_NULL, \n");
            varname1.Append("                PROJECTTYPE_NAME, \n");
            varname1.Append("                0 level \n");
            varname1.Append("         FROM   T_PUB_PROJECTTYPE \n");
            varname1.Append(string.Format("         WHERE  PARENT_ID = '{0}' \n", strPID));
            varname1.Append("         UNION ALL \n");
            varname1.Append("         SELECT A.PROJECTTYPE_ID, \n");
            varname1.Append("                A.PARENT_ID, \n");
            varname1.Append("                A.IS_NULL, \n");
            varname1.Append("                A.PROJECTTYPE_NAME,b.IDPATH, \n");
            varname1.Append("                b.level + 1 \n");
            varname1.Append("         FROM   T_PUB_PROJECTTYPE A, \n");
            varname1.Append("                lmenu b \n");
            varname1.Append("         WHERE  a.PROJECTTYPE_ID = b.PARENT_ID) \n");
            varname1.Append("SELECT * \n");
            varname1.Append("FROM   lmenu ");
            List<Model.SysManage.T_PUB_PROJECTTYPE> list = new List<Model.SysManage.T_PUB_PROJECTTYPE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, varname1.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        public List<string> GetProjectTypePath(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT t.IDPATH \n");
            varname1.Append("FROM   dbo.T_PUB_PROJECT p, \n");
            varname1.Append("       dbo.T_PUB_PROJECTTYPE t \n");
            varname1.Append("WHERE  p.PROJECTTYPE_ID = t.PROJECTTYPE_ID \n");
            if (strWhere!="")
            {
                varname1.Append(strWhere);
            }
            varname1.Append("GROUP  BY t.IDPATH ");
            List<string> list = new List<string>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, varname1.ToString()))
            {
               while(dataReader.Read())
                {
                    list.Add(dataReader.GetString(0));
                }
            }
            return list;
        }

        #region  Method
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_PROJECTTYPE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PROJECTTYPE(");
            strSql.Append("PROJECTTYPE_NAME,PARENT_ID,IS_NULL,IDPATH)");

            strSql.Append(" values (");
            strSql.Append("@PROJECTTYPE_NAME,@PARENT_ID,@IS_NULL,@IDPATH)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECTTYPE_NAME", DbType.String, model.PROJECTTYPE_NAME);
            db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
            db.AddInParameter(dbCommand, "IS_NULL", DbType.Boolean, model.IS_NULL);
            db.AddInParameter(dbCommand, "IDPATH", DbType.String, model.IDPATH);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_PROJECTTYPE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PROJECTTYPE set ");
            strSql.Append("PROJECTTYPE_NAME=@PROJECTTYPE_NAME,");
            strSql.Append("PARENT_ID=@PARENT_ID,");
            strSql.Append("IS_NULL=@IS_NULL,");
            strSql.Append("IDPATH=@IDPATH");
            strSql.Append(" where PROJECTTYPE_ID=@PROJECTTYPE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECTTYPE_ID", DbType.Int32, model.PROJECTTYPE_ID);
            db.AddInParameter(dbCommand, "PROJECTTYPE_NAME", DbType.String, model.PROJECTTYPE_NAME);
            db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
            db.AddInParameter(dbCommand, "IS_NULL", DbType.Boolean, model.IS_NULL);
            db.AddInParameter(dbCommand, "IDPATH", DbType.String, model.IDPATH);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PROJECTTYPE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PROJECTTYPE ");
            strSql.Append(" where PROJECTTYPE_ID=@PROJECTTYPE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECTTYPE_ID", DbType.Int32, PROJECTTYPE_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_PROJECTTYPE GetModel(int PROJECTTYPE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECTTYPE_ID,PROJECTTYPE_NAME,PARENT_ID,IS_NULL,IDPATH from T_PUB_PROJECTTYPE ");
            strSql.Append(" where PROJECTTYPE_ID=@PROJECTTYPE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECTTYPE_ID", DbType.Int32, PROJECTTYPE_ID);
            Model.SysManage.T_PUB_PROJECTTYPE model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECTTYPE_ID,PROJECTTYPE_NAME,PARENT_ID,IS_NULL,IDPATH ");
            strSql.Append(" FROM T_PUB_PROJECTTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_PROJECTTYPE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECTTYPE_ID,PROJECTTYPE_NAME,PARENT_ID,IS_NULL,IDPATH ");
            strSql.Append(" FROM T_PUB_PROJECTTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_PROJECTTYPE> list = new List<Model.SysManage.T_PUB_PROJECTTYPE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_PROJECTTYPE ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_PROJECTTYPE model = new Model.SysManage.T_PUB_PROJECTTYPE();
            object ojb;
            ojb = dataReader["PROJECTTYPE_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PROJECTTYPE_ID = (int)ojb;
            }
            model.PROJECTTYPE_NAME = dataReader["PROJECTTYPE_NAME"].ToString();
            ojb = dataReader["PARENT_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PARENT_ID = (int)ojb;
            }
            ojb = dataReader["IS_NULL"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IS_NULL = (bool)ojb;
            }
            model.IDPATH = dataReader["IDPATH"].ToString();
            return model;
        }

        #endregion  Method
    }
}

